package com.example.sprintboot.mapper;

import com.example.sprintboot.entity.WeChatToken;
import org.apache.ibatis.annotations.*;

/**
 * 微信Token Mapper接口
 */
@Mapper
public interface WeChatTokenMapper {
    
    /**
     * 根据AppID查询Token
     * @param appid 微信AppID
     * @return Token对象
     */
    @Select("SELECT * FROM wechat_token WHERE appid = #{appid} LIMIT 1")
    WeChatToken selectByAppid(String appid);
    
    /**
     * 插入新的Token
     * @param token Token对象
     */
    @Insert("INSERT INTO wechat_token (appid, access_token, expires_in, expire_time, create_time, update_time) " +
            "VALUES (#{appid}, #{accessToken}, #{expiresIn}, #{expireTime}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(WeChatToken token);
    
    /**
     * 更新Token（根据AppID）
     * @param token Token对象
     */
    @Update("UPDATE wechat_token SET " +
            "access_token = #{accessToken}, " +
            "expires_in = #{expiresIn}, " +
            "expire_time = #{expireTime}, " +
            "update_time = #{updateTime} " +
            "WHERE appid = #{appid}")
    void updateByAppid(WeChatToken token);
    
    /**
     * 删除Token（根据AppID）
     * @param appid 微信AppID
     */
    @Delete("DELETE FROM wechat_token WHERE appid = #{appid}")
    void deleteByAppid(String appid);
    
    /**
     * 根据ID查询Token
     * @param id 主键ID
     * @return Token对象
     */
    @Select("SELECT * FROM wechat_token WHERE id = #{id}")
    WeChatToken selectById(Integer id);
}

